package core.queue;

import core.router.Packet;
import core.schedulers.SchedulerKind;


public class RouterQueue {
	
	//EM Bytes
	private long capacity;
	private long currentLength;
	private SchedulerKind netInterface;
	
	private QueueNode head; 
	
	public RouterQueue(long c, SchedulerKind schedulerKind){
		this.capacity = c;
		this.netInterface = schedulerKind;
	}

	public boolean receivePacket(Packet p){
		if(currentLength + p.getSize() > capacity)
			return false;
		System.out.println("ADICIONOU UM PACOTE NA FILA DA INTERFACE DA " + netInterface);
		addPacket(p);
		return true;
	}

	private void addPacket(Packet p) {
		QueueNode newNode = new QueueNode(p);
		if(head == null){
			head = newNode;
			return;
		}
		QueueNode current = head;
		while(current.getNext() != null)
			current = current.getNext();
		current.setNext(newNode);
		currentLength += p.getSize();
	}
	
	public Packet removePacket(){
		System.out.println("REMOVENDO!!!");
		if(head == null)
			return null;
		System.out.println();
		System.out.println();
		return removeHead();
	}

	private Packet removeHead() {
		System.out.println("=== RETIRANDO UM PACOTE DA FILA DA INTERFACE DA " + netInterface + "===");
		System.out.println();
		System.out.println();
		Packet headPacket = head.getPacket();
		//Sleep de headPacket.getSize()/Bandwidth segundos
		QueueNode nextNode = head.getNext();
		head = nextNode;
		currentLength -= headPacket.getSize(); 
		return headPacket;
	}
	
	public boolean isEmpty(){
		return head == null;
	}
}
